<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>异常处理</title>
  </head>
  <body></body>
  <script>
    // demo1();
    demo2();

    // 触发异常，并捕获
    function demo1() {
      try {
        create_exception();
      } catch (e) {
        console.warn(e.toString());
      }
    }

    function demo2() {
      try {
        create_user_exception();
      } catch (e) {
        console.warn(e);
      }
    }

    // 抛出异常
    // 异常可以是任何类型的
    function create_exception() {
      throw "自定义异常";
    }
    // 抛出自定义异常
    function create_user_exception() {
      throw Error("自定义异常类测试");
    }

    // 创建自定义异常，用于精准捕获
    // TODO: 测试未生效
    function UserException(message) {
      Error.call(this, message);
      this.message = message;
      this.name = "UserException";
    }
  </script>
</html>
